<?php
declare(strict_types=1);

namespace App\AdminApi\Module\System\Service;

use App\AdminApi\Module\System\Traits\ConfigureTraits;
use App\Common\Model\System\SmsModel;
use Zms\AdminElementPlus\Component\Component;
use Zms\AdminElementPlus\FormCreate\FormItem;
use Zms\Basics\BasicsServe;
use Zms\Sms\Drive\Alsms;
use Zms\Sms\Drive\Fxsms;
use Zms\Sms\Drive\Ztsms;
use Zms\Sms\SmsResult;
use function Hyperf\Support\env;

/**
 * @method SmsModel getModel()
 */
class SmsService extends BasicsServe
{
    use ConfigureTraits;

    /**
     * @return SmsModel::class
     */
    public static function model(): string
    {
        return SmsModel::class;
    }


    /**
     * 添加短信发送记录
     * @param SmsResult $sendResult
     * @return void
     */
    public function addLog(SmsResult $sendResult): void
    {
        $this->created(
            [
                'template_id' => $sendResult->template_key,
                'driver' => $sendResult->driver,
                'phone' => $sendResult->phone,
                'content' => $sendResult->content,
                'status' => $sendResult->status ? 1 : 0,
            ]
        );
    }


    public function getSmsFormRule($configure): array
    {
        $SmsRule = [
            FormItem::Radio('open', '短信功能', $configure['open'] ?? '0')->options([
                ['value' => '0', 'label' => '关闭'],
                ['value' => '1', 'label' => '开启']
            ])->control([
                [
                    'value' => '0',
                    'rule' => [
                        Component::Alert('短信功能已关闭,将无法再发送短信', 'error')->style(['margin' => '0 0 20px 150px'])
                    ]
                ],
                [
                    'value' => '1',
                    'rule' => [
                        FormItem::Select('driver', '短信服务商', $configure['driver'] ?? '')->options([
                            ['value' => Fxsms::class, 'label' => '四川分享短信'],
                            ['value' => Ztsms::class, 'label' => '助通科技短信'],
                            ['value' => Alsms::class, 'label' => '阿里云短信'],
                        ])->control([
                            [
                                'value' => Fxsms::class,
                                'rule' => [
                                    Component::Alert('正在使用四川分享短信', 'success')->style(['margin' => '0 0 20px 150px']),
                                    FormItem::Input('username', '短信账号', $configure['username'] ?? '')->required(),
                                    FormItem::Input('password', '短信密码', $configure['password'] ?? '')->required(),
                                    FormItem::Input('sign', '短信签名', $configure['sign'] ?? '')->required()->hint('请确保签名已备案')
                                ]
                            ],
                            [
                                'value' => Ztsms::class,
                                'rule' => [
                                    Component::Alert('正在使用助通科技短信', 'success')->style(['margin' => '0 0 20px 150px']),
                                    FormItem::Input('username', '短信账号', $configure['username'] ?? '')->required(),
                                    FormItem::Input('password', '短信密码', $configure['password'] ?? '')->required(),
                                    FormItem::Input('sign', '短信签名', $configure['sign'] ?? '')->required()->hint('请确保签名已备案')
                                ]
                            ],
                            [
                                'value' => Alsms::class,
                                'rule' => [
                                    Component::Alert('正在使用阿里云短信', 'success')->style(['margin' => '0 0 20px 150px']),
                                    FormItem::Input('AccessKeyId', 'AccessKeyId', $configure['AccessKeyId'] ?? '')->required(),
                                    FormItem::Input('AccessKeySecret', 'AccessKeySecret', $configure['AccessKeySecret'] ?? '')->required(),
                                    FormItem::Input('sign', '短信签名', $configure['sign'] ?? '')->required()->hint('请确保签名已备案'),
                                    Component::Alert('登录阿里云，进入AccessKey管理页面创建或获取')->style(['margin' => '0 0 20px 150px']),
                                ]
                            ]
                        ])
                    ]
                ]
            ])
        ];
        if (env('SMS_DEBUG', false) === true) {
            $SmsRule[] = Component::Alert('短信提醒', 'warning')->description('当前短信处于开发调试模式,此模式不会真的发送短信，且输入任何验证码都会通过验证！')->style(['margin' => '0 0 20px 150px']);
        }
        return $SmsRule;
    }
}